package com.example.actordemo3.http;

//import akka.actor.ActorSystem;
//import akka.http.javadsl.ConnectHttp;
//import akka.http.javadsl.Http;
//import akka.http.javadsl.IncomingConnection;
//import akka.http.javadsl.ServerBinding;
//import akka.stream.ActorMaterializer;
//import akka.stream.Materializer;
//import akka.stream.javadsl.Sink;
//import scala.io.Source;

import akka.actor.ActorSystem;
import akka.http.javadsl.ConnectHttp;
import akka.http.javadsl.Http;
import akka.http.javadsl.IncomingConnection;
import akka.http.javadsl.ServerBinding;
import akka.stream.ActorMaterializer;
import akka.stream.Materializer;
import akka.stream.javadsl.Sink;
import akka.stream.javadsl.Source;

import java.util.concurrent.CompletionStage;

public class HttpDemo {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create();
        Materializer materializer = ActorMaterializer.create(system);

        Source<IncomingConnection, CompletionStage<ServerBinding>> serverSource =
                Http.get(system).bind(ConnectHttp.toHost("localhost", 8080), materializer);

        CompletionStage<ServerBinding> serverBindingFuture =
                serverSource.to(Sink.foreach(connection -> {
                            System.out.println("Accepted new connection from " + connection.remoteAddress());
                            // ... and then actually handle the connection
                        }
                )).run(materializer);
    }
}
